package com.xxw.yygh.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xxw.yygh.common.result.R;
import com.xxw.yygh.enums.OrderStatusEnum;
import com.xxw.yygh.enums.PaymentStatusEnum;
import com.xxw.yygh.model.order.OrderInfo;
import com.xxw.yygh.model.order.PaymentInfo;
import com.xxw.yygh.order.service.OrderInfoService;
import com.xxw.yygh.order.service.PaymentInfoService;
import com.xxw.yygh.order.service.WeiPayService;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/user/order/weixin")
public class WeiPayController {

    @Resource
    private WeiPayService weiPayService;

    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private PaymentInfoService paymentInfoService;

    //生成微信支付二维码
    @GetMapping("/{orderId}")
    public R createNative(@PathVariable Long orderId){
        String url=weiPayService.createNative(orderId);
        return R.ok().data("url",url);
    }

    //查询订单状态
    @GetMapping("/status/{orderId}")
    public R getPayStatus(@PathVariable Long orderId){
        Map<String,String> map=weiPayService.queryPayStatus(orderId);
        if (map ==null) {
            return R.error().message("查询失败");
        }
        if ("SUCCESS".equals(map.get("trade_state"))){
            orderInfoService.paySuccess(orderId,map);

            return R.ok();
        }
        return R.ok().message("支付中");
    }

}
